//
//  BHSubscriptionAnimationView.swift
//  APSwift
//
//  Created by jianbo.pei on 2025/3/21.
//

import UIKit

let topItemY: CGFloat = 4
let normalItemX: CGFloat = 70
let specialItemX: CGFloat = 26
let itemW: CGFloat = sw - normalItemX * 2
let itemH: CGFloat = 58

class BHSubscriptionAnimationView: UIView {
    private let itemTitles: [String] = [
        "I can help you quickly log your completed plans",
        "I’ll be with you every step of your weight loss journey",
        "Unlock AI chat sessions",
        "AI gives weight loss tips",
        "Got severe side effects? Don’t worry, I’ll help you manage them",
        "Solve all your problems with maxbud"
    ]
    
    private let itemViews = BHSubscriptionAnimationItemView.ItemIndex.allCases.map({ BHSubscriptionAnimationItemView(itemIdx: $0)})
    
    public private(set) var isAnimating: Bool = false

    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        backgroundColor = .lightText
        layer.masksToBounds = true
        
        itemViews.forEach { itemV in
            addSubview(itemV)
            let idx = itemV.itemIdx
            itemV.frame = CGRect(x: idx.normalX, y: idx.normalY, width: itemW, height: itemH)
            let tuple = itemV.itemIdx.tuple
            itemV.backgroundColor = tuple.bgColor
            itemV.titleLabel.font = tuple.textFont
            itemV.titleLabel.textColor = tuple.textColor
            itemV.titleLabel.text = itemTitles[vmSafe: itemV.itemIdx.rawValue]
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension BHSubscriptionAnimationView {
    public func startAnimation() {
        
        guard !isAnimating else {
            return
        }
        isAnimating = true
        
        let duration: TimeInterval = 2

        
        let secondTuple = BHSubscriptionAnimationItemView.ItemIndex.second.tuple
        let thirdTuple = BHSubscriptionAnimationItemView.ItemIndex.third.tuple
        
        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.65, initialSpringVelocity: 3.0) { [weak self] in
            guard let self else { return }
            self.itemViews.forEach { itemV in
                itemV.frame = CGRect(x: itemV.itemIdx.nextX, y: itemV.itemIdx.nextY, width: itemW, height: itemH)
            }
        } completion: { [weak self] _ in
            debugPrint("maxbud - usingSpringWithDamping completion")
            self?.isAnimating = false
            self?.startAnimation()
        }
        
        UIView.animate(withDuration: duration - 1) { [weak self] in
            guard let self else { return }
            self.itemViews.forEach { itemV in
                if itemV.itemIdx == .third {
                    itemV.titleLabel.font = secondTuple.textFont
                    itemV.titleLabel.textColor = secondTuple.textColor
                    itemV.backgroundColor = secondTuple.bgColor
                } else if itemV.itemIdx == .fourth {
                    itemV.titleLabel.font = thirdTuple.textFont
                    itemV.titleLabel.textColor = thirdTuple.textColor
                    itemV.backgroundColor = thirdTuple.bgColor
                }
            }
        } completion: { [weak self] _ in
            guard let self else { return }

            debugPrint("maxbud - animate(withDuration completion")
            
            /// 在没有重置itemIdx前，先把最上面的位移到最下面
            if let firstItemV = self.itemViews.first(where: { $0.itemIdx == .first }) {
                firstItemV.frame = CGRect(x: normalItemX, y: BHSubscriptionAnimationItemView.ItemIndex.sixth.normalY, width: itemW, height: itemH)
            }
            self.itemViews.forEach { itemV in
                itemV.resetItemIndex(itemV.itemIdx.nextIndex)
            }
        }

    }
}
